package com.markedquilt.comment.controller;

import com.markedquilt.comment.coomon.*;
import com.markedquilt.comment.model.UserModel;
import com.markedquilt.comment.request.LoginReq;
import com.markedquilt.comment.request.RegisterReq;
import com.markedquilt.comment.service.UserService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

/**
 * @author ZhangKe
 * @Description
 * @date 2020/4/17 23:20.
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    public static final String CURRENT_USER_SESSION = "currentUserSession";

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private UserService userService;

    @RequestMapping("/test")
    public String test(){
        return "success";
    }

    @RequestMapping("/index")
    @ResponseBody
    public ModelAndView index(){
        String userName="coke";
        ModelAndView modelAndView=new ModelAndView("/index.html");
        modelAndView.addObject("name",userName);
        return modelAndView;
    }

    @GetMapping("/get")
    @ResponseBody
    public CommonRes getUser(@RequestParam(name = "id",required =true)Integer id) throws BussinessException {
        UserModel userModel=userService.getUser(id);
        if (userModel==null)
            throw new BussinessException(EmBussinessError.NO_OBJECT_FOUND);
        else
            return CommonRes.create(userModel);
    }

    @PostMapping("/register")
    @ResponseBody
    public CommonRes register(@Valid @RequestBody RegisterReq registerReq, BindingResult bindingResult)
            throws UnsupportedEncodingException, NoSuchAlgorithmException, BussinessException {
        if (bindingResult.hasErrors()){
            throw new BussinessException(EmBussinessError.PARAMETER_VALIDATION_ERROR, CommonUtil.processErrorString(bindingResult));
        }
        UserModel userModel=new UserModel();
        userModel.setTelphone(registerReq.getTelphone());
        userModel.setPassword(registerReq.getPassword());
        userModel.setNickName(registerReq.getNickName());
        userModel.setGender(registerReq.getGender());

        UserModel resUserModel=userService.register(userModel);
        return CommonRes.create(resUserModel);
    }

    @RequestMapping("/login")
    @ResponseBody
    public CommonRes login(@RequestBody @Valid LoginReq loginReq,BindingResult bindingResult) throws BussinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
        if (bindingResult.hasErrors()){
            throw new BussinessException(EmBussinessError.PARAMETER_VALIDATION_ERROR,CommonUtil.processErrorString(bindingResult));
        }
        UserModel userModel=userService.login(loginReq.getTelphone(),loginReq.getPassword());
        httpServletRequest.getSession().setAttribute(CURRENT_USER_SESSION,userModel);
        return CommonRes.create(userModel);
    }

    @RequestMapping("/logout")
    @ResponseBody
    public CommonRes logout(){
        httpServletRequest.getSession().invalidate();
        return CommonRes.create(null);
    }

    @PostMapping("/getcurrentuser")
    @ResponseBody
    public CommonRes getCurrentUser(){
        UserModel userModel= (UserModel) httpServletRequest.getSession().getAttribute(CURRENT_USER_SESSION);
        return CommonRes.create(userModel);
    }
}
